<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-select v-model="selectItem.query" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-form-item>
          <el-input :placeholder="selectItem.query" v-model="selectItem.keywords" @keyup.enter.native="doSearch"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="doSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="background: #13ce66" v-on:click="_reload">刷新</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="fieldsData" border style="width: 100%" v-loading="loading" element-loading-text="玩命请求中。。。">
      <el-table-column prop="sn" label="期数" width="75">
      </el-table-column>
      <el-table-column prop="map_id" label="土地id" width="80">
      </el-table-column>
      <el-table-column prop="farm_id" label="农场" width="80" :formatter="farmName">
      </el-table-column>
      <el-table-column prop="area_name" label="区名" width="75">
      </el-table-column>
      <el-table-column prop="name" label="地名" width="75">
      </el-table-column>
      <el-table-column prop="status" label="种植信息" width="200">
        <template slot-scope="scope">
          <div class="field_list_left" v-if="scope.row.list_pic">
            <img :src="scope.row.list_pic" alt="">
          </div>
          <div class="field_list_right">
            <p>{{ scope.row.plant_maturity }}</p>
            <p>{{ scope.row.plant_name }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="浇水cd(时)">
        <template slot-scope="scope">
          <cdInput :val="scope.row.water_interval" :field_name="cd_name[0]" :sn="scope.row.sn"></cdInput>
        </template>
      </el-table-column>
      <el-table-column prop="fert_interval" label="施肥cd(时)">
        <template slot-scope="scope">
          <cdInput :val="scope.row.fert_interval" :field_name="cd_name[1]" :sn="scope.row.sn"></cdInput>
        </template>
      </el-table-column>
      <el-table-column prop="weed_interval" label="除草cd(时)">
        <template slot-scope="scope">
          <cdInput :val="scope.row.weed_interval" :field_name="cd_name[2]" :sn="scope.row.sn"></cdInput>
        </template>
      </el-table-column>
      <el-table-column prop="bug_interval" label="除虫cd(时)">
        <template slot-scope="scope">
          <cdInput :val="scope.row.bug_interval" :field_name="cd_name[3]" :sn="scope.row.sn"></cdInput>
        </template>
      </el-table-column>
      <el-table-column prop="production" label="产量(斤)" width="90" :formatter="changeProduction">
      </el-table-column>
      <el-table-column prop="price" label="单价(元)" width="90" :formatter="changePrice">
      </el-table-column>
      <el-table-column prop="sold" label="已售" width="70">
      </el-table-column>
      <el-table-column prop="inventory" label="份数" width="70">
      </el-table-column>
      <el-table-column prop="plant_time" label="种植时间" width="120">
      </el-table-column>
      <el-table-column prop="collect_time" label="预熟时间" width="120" :formatter="collectTime">
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="160" :formatter="createTime">
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentPageChange"
        :page-size="30"
        layout="prev, pager, next, jumper"
        :total="total" style="float:right;" :current-page="parseInt(selectItem.page)">
      </el-pagination>
      <span style="float: right;margin-top: 6px;margin-right: 5px">共 {{total}} 条数据</span>
    </el-col>

  </section>
</template>

<script type="text/ecmascript-6">
  import util from '../../util'
  import NProgress from 'nprogress'
  import cdInput from '../common/cdInput'
  // import { getplantLog } from '../../api'
	import { apiTree } from '../../apiTree'
  export default {
    name: 'field',
    // 局部注册组件
    components: {
      cdInput
    },
    data() {
      return {
        loading: false,
        options: [
          {value: 'plant_name', label: '菜品'},
          {value: 'area_name', label: '区域名'},
          {value: 'name', label: '地块名'}
        ],
        total: 50,
        selectItem: {
          page: this.$route.query.page !== undefined ? this.$route.query.page : 1,
          query: this.$route.query.query !== undefined ? this.$route.query.query : '',
          keywords: this.$route.query.keywords !== undefined ? this.$route.query.keywords : ''
        },
        fieldsData: [],
        cd_name: ['water_interval', 'fert_interval', 'weed_interval', 'bug_interval']
      }
    },
    methods: {
      waterCdTime(row) {
        return Math.ceil(parseInt(row.water_interval) / 3600)
      },
      fertCdTime(row) {
        return Math.ceil(parseInt(row.fert_interval) / 3600)
      },
      weedCdTime(row) {
        return Math.ceil(parseInt(row.weed_interval) / 3600)
      },
      bugCdTime(row) {
        return Math.ceil(parseInt(row.bug_interval) / 3600)
      },
      // 转变产量为斤
      changeProduction(row) {
        return Math.floor(parseInt(row.production) / 500)
      },
      // 转变单价为元
      changePrice(row) {
        return Math.floor(parseInt(row.price) / 100)
      },
      // 农场名称
      farmName(row, column) {
        return row.farm_id === '1' ? '枫泾' : '其他农场'
      },
      // 到期时间
      collectTime(row, column) {
        return row.expire === '0' ? 0 : this.formatTime(row.collect_time).substring(0, 10)
      },
      _reload() {
        this.getplantLog()
      },
      // 创建时间
      createTime(row) {
        return row.create_time.substring(2)
      },
      // 修改时间
      formatTime(_time) {
        return util.formatDate.format(new Date(_time * 1000), 'yyyy-MM-dd hh:mm:ss')
      },
      // 翻页
      currentPageChange(val) {
        this.selectItem.page = val
        this.$router.push({path: this.$route.path, query: this.selectItem})
      },
      // 搜索
      doSearch() {
        if (this.selectItem.query) {
          this.$router.push({path: this.$route.path, query: this.selectItem})
        }
      },
      getplantLog() {
        let para = this.$route.query
        if (util.isEmptyObject(para)) {
          this.selectOptions = {
            page: 1,
            query: '',
            keywords: ''
          }
        }
        this.loading = true
        NProgress.start()
        apiTree('plantLog/index', 'field/plantLog',para).then((res) => {
          if (res.s === 1) {
            this.total = parseInt(res.d.total_num)
            this.fieldsData = res.d.data
            this.loading = false
            NProgress.done()
          }
        })
      }
    },
    created() {
      this.getplantLog()
    },
    watch: {
      $route: 'getplantLog'
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .field_list_left
    display inline-block
    vertical-align middle
    margin-top 5px
    & img
      width 90px
      height 60px
  .field_list_right
    display inline-block
    vertical-align middle
    margin-left 5px
</style>
